package com.dailyblue.java.servlet.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

// Servlet的第三种实现方式 一般都使用这种方式 只能处理HTTP协议
// 3⃣️ 注册Servlet
@WebServlet(value = "/third", loadOnStartup = 2)
// 1⃣️ 继承 HttpServlet
public class ThirdController extends HttpServlet {

    // 2⃣️ 重写service方法 request和response对象是Tomcat送过来
    /*protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("ThirdController被调用了");
        // 设置响应的编码集 必须在out对象产生之前
        response.setCharacterEncoding("utf-8");
        // 输出一个页面出去 获取到out对象
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<meta charset='utf-8'>");
        out.println("<title>Servlet输出的内容</title>");
        out.println("</head>");
        out.println("<body>");
        int num = (int) (Math.random() * 100 + 1);
        out.println("<h3>数字是： " + num + "</h3>");
        out.println("</body>");
        out.println("</html>");
    }*/

    // 只能处理get请求操作
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("我处理了get请求");
    }

    // 只能处理post请求操作
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("我处理了post请求");
    }
}
